class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> hash1(26), hash2(26), ret;
        for(auto& e : p)
        {
            hash2[e - 'a']++;
        }
        int cnt = 0, n1 = s.size(), n2 = p.size(), left = 0, right = 0;
        if(n1 < n2)
            return {};
        while(right < n1)
        {
            char in = s[right];
            if(++hash1[in - 'a'] <= hash2[in - 'a']) // 进窗口
                ++cnt;

            if(right - left + 1 > n2) // 出窗口
            {
                char out = s[left++];
                if(hash1[out - 'a']-- <= hash2[out - 'a'])
                    --cnt;
            }
            if(cnt == n2) // 更新结果
                ret.push_back(left);
            ++right;
        }
        return ret;
    }
};